package cn.yoyo.dao;

import cn.yoyo.entity.Like;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class LikeDaoImpl implements LikeDao{
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public Like add(Like like) {
         return mongoTemplate.insert(like);
    }

    @Override
    public Like findById(String blogId,String userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("blogId").is(blogId));
        query.addCriteria(Criteria.where("userId").is(userId));
        return mongoTemplate.findOne(query,Like.class);
    }

    @Override
    public List<Like> findMyLikes(String userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.find(query,Like.class);
    }

    @Override
    public void delete(String likeId) {
        Query query = new Query(Criteria.where("id").is(likeId));
        mongoTemplate.findAndRemove(query,Like.class);
    }
}
